import axios, { AxiosRequestConfig } from 'axios'

export class Axios {
   private readonly instance

   constructor(config: AxiosRequestConfig) {
      this.instance = axios.create(config)
      this.interceptors()
   }

   public request<T, D = ResponseData<T>>(config: AxiosRequestConfig) {
      return new Promise(async (resolve, reject) => {
         try {
            const res = await this.instance.request<D>(config)
            resolve(res.data)
         } catch (error) {
            reject(error)
         }
      }) as Promise<D>
   }

   private interceptors() {
      this.requestInterceptor()
      this.responseInterceptor()
   }

   private requestInterceptor() {
      this.instance.interceptors.request.use(
         (config) => {
            return config
         },
         (error) => {
            return error
         }
      )
   }

   private responseInterceptor() {
      this.instance.interceptors.response.use(
         (response) => {
            return response
         },
         (error) => {
            return error
         }
      )
   }
}
